#include "threadPool.h"
#include <stdio.h>
#include <unistd.h>

void func(void * arg)
{
    if(arg == NULL)
    {
        perror("arg is null");
    }
    struct NTASK* task = (struct NTASK*)arg;
    printf("hello world -- %d\n", *((int*)task->user_date));

    free(task->user_date);
    free(task);
}

int main(void)
{
    nthreadpool_t pool;
    int nthread = 5;

    int ret = thread_pool_create(&pool, nthread);

    for(int i = 0; i < 100; i++)
    {
        if(i == 50)
        {
            puts("------------------------------------");
            sleep(3);
            printf("task num: %d\n", get_task_size(&pool));
        }

        struct NTASK* task = (struct NTASK*)malloc(sizeof(struct NTASK));
        if(task == NULL) return -1;

        task->user_date = malloc(sizeof(int));
        if(task->user_date == NULL) return -2;

        *((int*)task->user_date) = i;
        task->next = task->prev = NULL;
        task->task_func = func;

        i % 2 == 0 ?  thread_pool_push_back(&pool, task) : thread_pool_push_front(&pool, task);
    }

    // sleep(10);
    thread_pool_destroy(&pool);


    struct NTASK* task = (struct NTASK*)malloc(sizeof(struct NTASK));
    if(task == NULL) return -1;

    task->user_date = malloc(sizeof(int));
    if(task->user_date == NULL) return -2;

    *((int*)task->user_date) = 100000;
    task->next = task->prev = NULL;
    task->task_func = func;

    ret = thread_pool_push_front(&pool, task);
    printf("a = %d\n", ret); //此任务应添加失败


    return 0;
}